﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web_Oto.Business;
using Web_Oto.Common;

namespace Web_Oto
{
    public partial class NewsDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                GetNewsDetail();
        }

        //private void GetSeoInformations(string title, string key_descript, Web_Oto.Data.New news)
        private void GetSeoInformations(Web_Oto.Data.New news)
        {
            //var seoBus = new SeoBus();
            //var seo = seoBus.GetSeoPage("news");
            //if (seo != null)
            //{
            //    Page.Title = title + " | " + seo.Title;
            //    Page.MetaDescription = key_descript + seo.Description;
            //    Page.MetaKeywords = key_descript + seo.Keyword;
            //}
            //else
            //{
            //    Page.Title = title;
            //    Page.MetaDescription = key_descript;
            //    Page.MetaKeywords = key_descript;
            //}
            Page.Title = news.SEO_Title;            
            Page.MetaKeywords = news.SEO_Keyword;
            Page.MetaDescription = news.SEO_Description;
        }

        private void GetNewsDetail()
        {
            var newsId = (Page.RouteData.Values.ContainsKey("newsId"))
                             ? Page.RouteData.GetRequiredString("newsId")
                             : null;
            if(newsId != null)
            {
                var newsBus = new NewsBus();
                var key_descript = "";
                var newsDetail = newsBus.GetNewsById(int.Parse(newsId));
                lblTitle.Text = lblNewsName.Text = newsDetail.Name;
                ltrIntro.Text = newsDetail.Intro;
                lblDatePost.Text = Convert.ToDateTime(newsDetail.Date).ToString("dddd, dd MMMM  yyyy");
                var path = ConfigurationManager.AppSettings["newsPath"];
                imgAvatar.ImageUrl = path +"/"+ newsDetail.Image;
                imgAvatar.AlternateText = newsDetail.TitleOfImage;
                ltrDescription.Text = newsDetail.Description;
                AnotherNews(int.Parse(newsId), newsBus, out key_descript);
                //GetSeoInformations(newsDetail.Name, key_descript);
                GetSeoInformations(newsDetail);
            }
            else
                Response.Redirect("/tin-tuc");
        }

        private void AnotherNews(int newsId, NewsBus newsBus, out string key_descript)
        {
            var lists = newsBus.GetAnotherNews(newsId);
            var sb = new StringBuilder();
            var str = "";
            foreach (var list in lists)
            {
                sb.AppendFormat("<li><a href='/tin-tuc/{0}/{1}.html'>{2}</a></li>", CommonHelper.NiceUrl(CommonHelper.RemoveSpecialChars(list.Name)), list.Id,list.Name);
                str += CommonHelper.NiceUrl(list.Name) + ", " + list.Name + ",";
            }
            key_descript = str;
            ltrAnotherNews.Text = sb.ToString();
        }
    }
}